<script type="text/javascript">
$(function(){
	$("#tipDialog").dialog({
		autoOpen:false,
		modal:true,
		width:450,
		show:"slide",
		buttons:{
			"Close":function(){
				$(this).dialog("close");
			}
		}
	});
	
	$("#tipDialog2").dialog({
		autoOpen:false,
		modal:true,
		width:450,
		show:"slide",
		buttons:{
			"Close":function(){
				$(this).dialog("close");			
			}
		}
	});

	$(".checkAll").click(function(){
		$(".checked").attr("checked",$(this).attr("checked")=="checked"?true:false);
	});
	
	$(".prdouctDiv2").dialog({
		autoOpen:false,
		width:700,
		height:400,
		modal:true,
		show:"slide",
        zIndex:200,
        buttons:{
            "Close": function() {$(this).dialog('close');}
        }
	});
	
});

function OpenProductDiv2(lineKey){
    $(".prdouctDiv2").dialog('open');
    initDataUploadPage(0,20,lineKey);	//批量上传页面的加载函数
}

function loadDataUploadPage(page,pageSize,lineKey){

    product_title = $("#searchForm2 [name=product_title]").val();
    product_sku = $("#searchForm2 [name=product_sku]").val();
    warehouse_id = $("#batchAddForm [name=warehouseId]").val();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/merchant/warehouse-products/index",
        data:{
            'page':page,
            'pageSize':pageSize,
            'product_sku':product_sku,
            'product_status':1,
            'product_title':product_title,
            'warehouse_id':warehouse_id
         },
        success: function(json) {
        if(typeof json.ask != "undefined" && json.ask){
            var html ="";
            paginationTotal = json.list.count;
            if (typeof json.list.list == "undefined") return;
            $.each(json.list.list,function(k,v){
                html +='<div class="row height25">';
                html +='<input type="hidden" name="declared_value[]" id="declaredValue'+v.product_id+'" value="'+v.product_declared_value+'" />';
                html +='<input type="hidden" name="unit_price[]" id="unitPrice'+v.product_id+'" value="'+v.product_declared_value+'" />';
                html +='<input type="hidden" name="op_description[]" id="opDescription'+v.product_id+'" value="" />';
                html +='<div class="cell " style="width:22%" id="productTitle'+v.product_id+'">'+v.product_title+'</div>';
                html +='<div class="cell " style="width:18%" id="productSku'+v.product_id+'">'+v.product_sku+'</div>';
                html +='<div class="cell " style="width:15%">'+v.product_status+'</div>';
                html +='<div class="cell " style="width:18%" id="pocValue'+v.product_id+'">'+v.category.poc_value+'</div>';
                html +='<div class="cell" style="width:12%" id="productWeight'+v.product_id+'">'+v.product_weight+'</div>';
				if($('.product'+v.product_id+'Line'+lineKey).length > 0){
					html += "<div class='cell2 ' style='width:15%'><img src='/images/yes.gif'></div>";
				}else{
					html +="<div class='cell2 ' style='width:15%'><span class='button-2' fkey='"+lineKey+"' onclick='addToTable("+v.product_id+",this)'>Add</span>";
					html +="<input value='"+v.product_weight+"' type ='hidden' id='product_weight"+v.product_id+"'></div>";
				}
                html +="</div>";
            });
            $("#tab-div2").html(html);
         }
      }
    });
}


function addToTable(product_id,obj){
	var lineKey = $(obj).attr('fkey');
    var product_title =$('#productTitle'+product_id).html();
    var product_sku =$('#productSku'+product_id).html();		//	sku Name
    var category =$('#pocValue'+product_id).html();			//	category 类别
    var product_weight = $('#productWeight'+product_id).html();
    var op_description = $('#opDescription'+product_id).val();
    var op_declared_value = $('#declaredValue'+product_id).val();
    var op_unit_price = $('#unitPrice'+product_id).val();
    
    var html = '';
    html += '<tr class="productLine'+product_id+'">';
    html += '<td>'+product_title+'</td>';	//SKU 产品名称
    html += '<td><input type="hidden" class="inputText product_sku2"  name="product_sku2[]" value="'+product_sku+'" style="width:100px" readonly/>'+product_sku+'</td>';
    html += '<td>'+category+'</td>';
    
    html += '<td fname="op_quantity" fkey="'+lineKey+'" fsku="'+product_sku+'">';
    html += '<input type="text" class="inputText" name="submitQuantity[]" value="1" style="width:80px;text-align:right" onblur="quantitySave(this)" /></td>';
    html += '<td>'+product_weight+'</td>';
    html += '<td><img src="/images/icon_del.gif" name="button'+product_sku+'" onclick="delProduct(this)" class="cursor" title="Delete"/></td>';
    html += '</tr>';
    
    $('#productTable'+lineKey).append(html);
	$(obj).parent().html("<img src='/images/yes.gif'>");

	$.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/merchant/orders/product-batch-add",
        data:{'orderJson':orderJson,'product_sku':product_sku,'product_title':product_title,
        	  'op_quantity':1,'op_description':op_description,'category':category,
        	  'product_id':product_id,'op_declared_value':op_declared_value,
        	  'op_unit_price':op_unit_price,'op_weight':product_weight,
        	  'lineKey':lineKey
        		 },
        success: function(json) {
        	if(!json.ask){				
				$("#messageTip").dialog("open");			
				$("#messageTipOutput").html('产品添加错误');
			}else {
				orderJson = json;				
			}        	
        }
	});
	
	$(".productError").hide();
}

function delProduct(obj){
	loading();
	var key = $(obj).attr('fkey');
	var product_sku = $(obj).attr('fvalue');
    $(obj).parent('td').parent('tr').remove();
    productDelete(key,product_sku);
    closeLoading();
}

//触发产品数量保存
function quantitySave(obj){
	loading();
	var key = $(obj).parent().attr('fkey');
	var product_sku = $(obj).parent().attr('fsku');
	var value = $.trim($(obj).val());
	$.each(orderJson.data[key]['order_product'],function(key2,item2){
		if(product_sku == item2.product_sku){
			orderJson.data[key]['order_product'][key2]['op_quantity'] = value;
		}
	});
	closeLoading();
}

//触发批量上传时 产品数据删除
function productDelete(key,product_sku){
	var order_product=[];
	$.each(orderJson.data[key]['order_product'],function(key2,item2){
		if(product_sku != item2.product_sku){
			order_product.push(orderJson.data[key]['order_product'][key2]);
		}
	});
	orderJson.data[key]['order_product'] = order_product;
}


function initDataUploadPage(pageIndex,pageSize,lineKey) {
	loading();
	if(typeof(paginationTotal)!='undefined'){	
		if(Math.ceil(paginationTotal/paginationPageSize) <= paginationCurrentPage){
			paginationCurrentPage = Math.ceil(paginationTotal/paginationPageSize);
			pageIndex = paginationCurrentPage - 1;
		}
	}
	loadDataUploadPage(pageIndex + 1,paginationPageSize,lineKey);
	closeLoading();
	if(pageIndex <0){pageIndex = 0;}
	  //加入分页的绑定 
	$(".pagination2").pagination(paginationTotal, { 
		  callback: pageselect,
		  first_text:'First',
		  prev_text: '< Previous', 
		  next_text: 'Next >', 
		  last_text: 'Last',
		  perpage_text: 'PerPage',
		  total_text: 'Total',
		  items_per_page: paginationPageSize, 
		  num_display_entries: 6, 
		  current_page: pageIndex, 
		  num_edge_entries: 2 
	 }); 
}

function pageselect(page_id, jq) { 
	paginationCurrentPage = page_id+1;
	initDataUploadPage(page_id); 
}

function changePageSize(obj){
	paginationPageSize = $(obj).val();
	if(paginationPageSize <= 0){
		paginationPageSize = 20;
	}
	
	if(Math.ceil(paginationTotal/paginationPageSize) <= paginationCurrentPage){
		paginationCurrentPage = Math.ceil(paginationTotal/paginationPageSize);
	}
	if(paginationCurrentPage == 0){
		paginationCurrentPage = 1;
	}
	initDataUploadPage(paginationCurrentPage-1);
}

function productUploadPage(){
    initDataUploadPage(0);
}

</script>
<style type="text/css">
div.checkDiv{
	overflow-x:auto;
}

.myTab td, .myTab th {
    border: 1px solid #AECED9;
    padding: 3px;
    text-align: center;
}
.myTab td.addrTd{
	text-align: left;
}
div.address{
	float:left;
	width:80%;
}
div.addrDiv{
	float:left;
}
</style>
<div class="position"><a>Home</a> > <a>Main Page</a> > <a>CurrentPage</a> >  订单批量上传</div>
<div style="font-weight:bold;">订单批量上传数据检查</div><br />

<div style="color:red;" class="error"> </div>

<div style="margin-top:10px;margin-bottom:10px;">
	<span class="button-2 toggleButton"  status="1"> Unfold </span> &nbsp;&nbsp;
	<span class="button-2" onclick="uploadCheckData()"> Check Data </span> &nbsp;&nbsp;
	<span class="button-2" onclick="batchSubmit()" id="batchAddBtn" style="display:none"> Submit </span> &nbsp;&nbsp;
</div>

<div class="checkDiv">
<form id="batchAddForm" name="batchAddForm">
<input type="hidden" class="inputText" name="warehouseId" value="1"/>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab"  id="orderInfoTable">	
    <thead>
    <tr style="background:#F7B64B">			
		<th width="3%"><input class="checkAll" type="checkbox" /></th>
		<th width="8%">ReferenceNo</th>
	    <th width="7%">Shipping Method</th>
	    <th width="8%">Country</th>        
	    <th width="8%">First Name</th>	    
	    <th width="8%">Last Name</th>
	    
	    <th width="8%">City</th>
	    <th width="8%">State/Provice</th>        
	    <th width="8%">Postalcode</th>      
	    <th width="8%">Email</th>      
	    <th width="8%">Company</th>      
	    <th width="6%">PhoneNo</th>
	    <th width="4%">Action</th>  
	</tr>
    </thead>
	<tbody id="orderInfoList">
	
	</tbody>
</table>
</form>
<br />
</div>

<div id="tipDialog" style="display:none" title="Tip"></div>
<div id="tipDialog2" style="display:none" title="Tip"></div>

<div class="prdouctDiv2" style="display:none" title="SKUList">
	<{include file='merchant/orders/products-list2.tpl'}>
</div>